package com.bandit.controller;

import com.bandit.pojo.Blogger;
import com.bandit.service.BloggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BloggerController {

    @Autowired
    BloggerService bloggerService;

    @RequestMapping("/toPasswordModify")
    public ModelAndView toPwdModify(HttpSession session){
        ModelAndView mv = new ModelAndView();
        String loginUser = (String) session.getAttribute("LoginUser");
        Blogger blogger = bloggerService.getDetailsBySession(loginUser);
        mv.addObject("blogger",blogger);
        mv.setViewName("/back/pwdModify");
        return mv;
    }

    @RequestMapping("/PwdModify")
    @ResponseBody
    public Map<String,Object> PwdModify(@RequestParam("username") String username,
                                        @RequestParam("newPwd") String password){
        HashMap<String, Object> map = new HashMap<>();
        bloggerService.ModifyPassword(username,password);
        map.put("status","ok");
        return map;
    }

}
